#include "t_box_downloader.h"

#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include "xmlutil.h"
#include "yt_web.h"

T_Box_Downloader *T_Box_Downloader::fs_T_Box_Downloader = 0;
QNetworkAccessManager *T_Box_Downloader::fs_QNetworkAccessManager = 0;

void T_Box_Downloader::setNAM(QNetworkAccessManager *a_nam)
{
    fs_QNetworkAccessManager = a_nam;
}

T_Box_Downloader *T_Box_Downloader::instance()
{
    if (!fs_T_Box_Downloader) {
        fs_T_Box_Downloader = new T_Box_Downloader();
    }
    return fs_T_Box_Downloader;
}

T_Box_Downloader::T_Box_Downloader()
    : f_last_reply(NULL)
{
    qDebug() << "[T_Box_Downloader::T_Box_Downloader()]";
    Q_ASSERT(fs_QNetworkAccessManager);
    f_nam = fs_QNetworkAccessManager;
    f_tray_icon = new QSystemTrayIcon(qApp);
    f_tray_icon->setIcon(QIcon(QLatin1String(":128x128/arora.png")));
    connect(qApp, SIGNAL(aboutToQuit()), f_tray_icon, SLOT(hide()));
    connect(f_tray_icon, SIGNAL(messageClicked()), this, SLOT(slot_tray_icon_messageClicked()));
    f_tray_icon->show();

}

QNetworkAccessManager *T_Box_Downloader::getNAM()
{
    return f_nam;
}

void T_Box_Downloader::checkUrl(QUrl a_url)
{
    qDebug() << "[T_Box_Downloader::checkUrl] a_url" << a_url;
    qDebug() << "[T_Box_Downloader::checkUrl] a_url.host()" << a_url.host();
    QString v_host = a_url.host();
    QString v_path = a_url.path();
    QString v_fragment = a_url.fragment();

    QRegExp v_rx("^([^.]+)[.]youtube[.]com$");
    int v_pos = v_rx.indexIn(v_host, 0);
    qDebug() << "[T_Box_Downloader::checkUrl] v_pos" << v_pos;
    qDebug() << "[T_Box_Downloader::checkUrl] v_rx.cap(1)" << v_rx.cap(1);
    if(v_pos != -1)
    {
        qDebug() << "[T_Box_Downloader::checkUrl] v_path" << v_path;
        QRegExp v_rx_user("^/user/[^/]+[/]?$");
        QRegExp v_rx_watch("^/watch[/]?$");
        if(v_rx_user.indexIn(v_path, 0)==0)
        {
            qDebug() << "[T_Box_Downloader::checkUrl] /user/----#" << v_fragment;
            QRegExp v_rx_fragment("/([-_A-Za-z0-9]+)$");
            v_rx_fragment.indexIn(v_fragment, 0);
            QString v_fragment_id = v_rx_fragment.cap(1);
            if(v_fragment_id.length()==11)
            {
                qDebug() << "[T_Box_Downloader::checkUrl] /user/----#----/" << v_fragment_id;
                handleVideoId(v_fragment_id);
            }
        }
        else if(v_rx_watch.indexIn(v_path, 0)==0)
        {
            QString v_watch_id = a_url.queryItemValue("v");
            qDebug() << "[T_Box_Downloader::checkUrl] /watch?----" << v_watch_id;
            handleVideoId(v_watch_id);
        }
    }
}

void T_Box_Downloader::handleVideoId(QString a_id)
{
    qDebug() << "[T_Box_Downloader::handleVideoId]" << a_id;
    T_Box_Video_Reply *v_box_reply = new T_Box_Video_Reply(this, a_id);
    connect(v_box_reply, SIGNAL(finished()), this, SLOT(self_on_reply_finished()));
}

void T_Box_Downloader::self_on_reply_finished()
{
    T_Box_Video_Reply *v_box_reply = (T_Box_Video_Reply *)sender();
    f_last_reply = v_box_reply;
    qDebug() << "[v_box_reply->f_title]" << v_box_reply->f_title;
    f_tray_icon->showMessage("ダウンロードしますか？",
                             v_box_reply->f_title + " / " +QString::number(v_box_reply->f_view_count),
                             QSystemTrayIcon::Information, 10 * 1000);
}

void T_Box_Downloader::slot_tray_icon_messageClicked()
{
    QMessageBox::information(0, tr("Systray"),
                             tr("Sorry, I already gave what help I could.\n"
                                "Maybe you should try asking a human?"));
}
